import request from '@/utils/request'
import Layout from '@/layout'

export default {

  /**
   * 获取所有菜单信息
   * @returns {AxiosPromise}
   */
  findAllAuthMenu() {
    return request({
      url: `/auth/auth/auth-menu/findAllAuthMenu`,
      method: 'get'
    })
  },
  /**
   * 菜单添加
   * @param data
   * @returns {AxiosPromise}
   */
  addAuthMenu(data) {
    return request({
      url: `/auth/auth/auth-menu/`,
      method: 'post',
      data: data
    })
  },
  /**
   * 获取动态路由表菜单信息
   * @returns {AxiosPromise}
   */
  queryAllMenuRouter() {
    return request({
      url: `/auth/auth/auth-menu/queryAllMenuRouter`,
      method: 'get'
    })
  },

  /**
   * 过滤路由信息
   * @param data
   * @returns {*[]}
   */
  filterRouterData(data) {
    let res = []
    for (let i = 0; i < data.length; i++) {
      let routerData = {
        component: null,
        alwaysShow: true,
        meta: {
          icon: '',
          title: '',
          affix: false
        },
        path: '',
        name: '',
        children: [],
        hidden: false
      }
      if (data[i].component == 'Layout') {
        routerData.component = Layout
      } else {
        // routerData.component =()=>import(data[i].component)
        routerData.component =loadView(data[i].component);
      }
      if (data[i].parentId!=0){
        delete routerData.alwaysShow
      }
      routerData.path = data[i].path
      if (data[i].name && data[i].name !== '') {
        routerData.name = data[i].name
      } else {
        delete routerData.name
      }

      if (data[i].meta && data[i].meta !== null) {
        if (data[i].meta.icon && data[i].meta.icon !== '') {
          routerData.meta.icon = data[i].meta.icon
        } else {
          delete routerData.meta.icon
        }

        if (data[i].meta.title && data[i].meta.title !== '') {
          routerData.meta.title = data[i].meta.title
        } else {
          delete routerData.meta.title
        }

        if (data[i].meta.affix && data[i].meta.affix !== '') {
          let flag=false;
          if (data[i].meta.affix=='true'){
            flag=true
          }
          routerData.meta.affix =flag
        } else {
          delete routerData.meta.affix
        }
        if (data[i].meta.affix == ''  && data[i].meta.icon == '') {
          console.log("data:",data[i])
          routerData.hidden = true
        }
      } else {
        delete routerData.meta
      }
      if (data[i].children !== null && data[i].children.length > 0) {
        routerData.children = this.filterRouterData(data[i].children)
      } else {
        delete routerData.children
      }
      res.push(routerData)
    }
    return res
  },

  /**
   * 根据菜单Id查询对应的资源权限信息
   * @param menuId
   * @returns {AxiosPromise}
   */
  queryAllAuthResource(menuId){
    return request({
      url:`/auth/auth/auth-resource/queryAllAuthResource/${menuId}`,
      method:'get'
    })
  },

  /**
   * 添加或 修改权限
   * @param data
   * @returns {AxiosPromise}
   */
  addOrUpdateAuthResource(data){
    return request({
      url:`/auth/auth/auth-resource/addOrUpdateAuthResource`,
      method:'post',
      data:data
    })
  }
}

export const loadView = view => {
  // 路由懒加载
  return resolve => require(['@/views/'+view], resolve)
}
